package com.concurrency.example.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class SynchronizedExample1 {
    public void test1(){
        synchronized (SynchronizedExample1.class) {
            for (int i = 0; i < 10; i++) {
                log.info("test-1:{}", i);
            }
        }
    }

    public synchronized void test2() {
        for (int i = 0; i < 10; i++) {
            log.info("test-2:{}", i);
        }
    }

    public static void main(String[] args) {
        SynchronizedExample1 s1 = new SynchronizedExample1();
        SynchronizedExample1 s2 = new SynchronizedExample1();
        ExecutorService executor = Executors.newCachedThreadPool();
        executor.execute(() -> {
            s1.test1();
        });
        executor.execute(() -> {
            s2.test1();
        });
    }
}
